<template>
  <div>
    <el-row class="insuranceBg">
      <el-col :span="4">
        <div class="mt-20">
          <el-tree ref="tree" :data="treeData1" :props="treeProps" :expand-on-click-node="false" default-expand-all
            highlight-current @node-click="handleNodeClick1">
            <div class="custom-tree-node" slot-scope="{ node, data }">
              <el-tooltip effect="dark" :content="node.label" placement="bottom" v-if="node.label.length > 8">
                <span class="label-info">{{ node.label }}</span>
              </el-tooltip>
              <span v-else>{{ node.label }}</span>
              <el-dropdown trigger="click" v-if="data.id">
                <i class="el-icon-more" @click.stop></i>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native="itemEdit1(node, data)" style="color: #082aff"
                    v-hasPermi="[`operate:companyVerbal:editGroup`]">编辑
                  </el-dropdown-item>
                  <el-dropdown-item @click.native="itemDel1(node, data)" style="color: #082aff"
                    v-hasPermi="[`admin:risk:remove`]">删除
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <span class="add" @click="handleAddItem1" v-if="!data.id" v-hasPermi="[`operate:companyVerbal:addGroup`]">
                <i class="el-icon-circle-plus-outline"></i>&nbsp;添加
              </span>
            </div>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20" style="border-left: 1px solid #eee; padding: 16px 15px 20px 15px;">
        <div style="border-bottom: 1px solid #eee">
          <NormalForm pageTitle="险种库" :isShowCard="false" :operateForm="queryForm" :formList="formList" @handleQuery="handleQuery" @handleReset="handleReset"></NormalForm>
        </div>
        <div style="text-align: right; margin: 20px 0;">
          <el-button type="primary" @click="goRoute" icon="el-icon-plus" v-hasPermi="['admin:risk:add']">创建险种</el-button>
        </div>
        <div>
          <el-table v-loading="loading" :data="tableList" style="width: 100%" height="380">
            <el-table-column type="index" width="55" align="center" label="序号" />
            <el-table-column label="险种图片" align="center" prop="coverImgUrl" key="coverImgUrl" show-overflow-tooltip>
              <template slot-scope="{ row }">
                <div class="table-img">
                  <img :src="row.coverImgUrl" />
                </div>
              </template>
            </el-table-column>
            <el-table-column label="名称" prop="riskName" align="center" show-overflow-tooltip>
              <template slot-scope="{ row }">
                <span style="color: #082aff" @click="detailRoute(row.riskId)">{{ row.riskName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="发送总次数" align="center" prop="sendNum" show-overflow-tooltip />
            <el-table-column label="创建人" align="center" prop="createBy" show-overflow-tooltip>
              <template slot-scope="{ row }">
                <span style="color: #082aff">{{ row.createBy }}</span>
              </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime" show-overflow-tooltip>
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" width="120" class-name="small-padding fixed-width">
              <template slot-scope="{ row }">
                <div style="display: flex; justify-content: space-around">
                  <el-button size="mini" type="text" @click="shareRoute" class="tag-copy"
                    :data-clipboard-text="row.riskLink" v-hasPermi="['admin:risk:share']">分享
                  </el-button>
                  <el-button size="mini" type="text" @click="editRoute(row.riskId)"
                    v-hasPermi="['wecom:material:edit:corp']">编辑
                  </el-button>
                  <el-button size="mini" type="text" @click="deleteItem(row.riskId)"
                    v-hasPermi="['wecom:material:delete:corp']">删除
                  </el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <NormalPagination :currentPage="queryForm.pageNum" :pageSize="queryForm.pageSize" :total="total" @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange"></NormalPagination>
        </div>
      </el-col>
    </el-row>
    <el-dialog :title="treeForm.id ? '编辑' : '添加' + '分组'" :visible.sync="treeDialogVisible" width="400px"
      :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form ref="treeForm" :model="treeForm" label-width="120px">
        <el-form-item label="请输入分组名称">
          <el-input v-model="treeForm.name" maxlength="32" show-word-limit placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="info" plain @click="treeDialogVisible = false">取 消
        </el-button>
        <el-button type="primary" @click="treeSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 删除分组 -->
    <el-dialog :title="'删除' + (treeForm.id ? '失败' : '确认')" :visible.sync="delVisible" width="400px"
      :close-on-click-modal="false" :close-on-press-escape="false">
      <span>确认删除该分组？</span>
      <div slot="footer">
        <el-button type="info" plain @click="delVisible = false">取 消
        </el-button>
        <el-button type="primary" @click="treeDelSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 删除table数据 -->
    <el-dialog title="删除" :visible.sync="delTable" width="400px" :close-on-click-modal="false"
      :close-on-press-escape="false">
      <span>确认删除该条数据吗？</span>
      <div slot="footer">
        <el-button type="info" plain @click="cancelDel">取 消</el-button>
        <el-button type="primary" @click="deleteLink">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { categoryAddCorp, categoryEditCorp, categoryList } from '@/api/article/group'
import { categoryDel, deleteLink, riskList } from '@/api/communityOperating/insurance'

export default {
  name: 'Insurance',
  data() {
    return {
      queryForm: {
        riskName: '',
        deptId: null,
        createCodeList: '',
        pageNum: 1,
        pageSize: 20,
      },
      formList: [
        {
          label: '险种名称',
          value: '',
          prop: 'riskName',
          type: 'INPUT',
          emunName: ''
        },
        {
          label: '管理机构',
          value: null,
          prop: 'deptId',
          type: 'MANAGE_COM',
          emunName: ''
        },
        {
          label: '创建人',
          value: '',
          prop: 'createCodeList',
          type: 'STAFF',
          emunName: ''
        },
      ],
      total: 0,
      loading: false,
      treeDialogVisible: false,
      delVisible: false,
      shareVisible: false,
      delTable: false, // 删除table数据
      riskId: '', // id
      cbFun: null,
      tableList: [],
      staffOptions: [], // 执行员工
      queryCreateList: [], // 创建人
      ruleList: [], // 任务名称
      nowTime: [], // 当前时分秒
      treeData1: [
        {
          id: '',
          name: '全部',
          parentId: 0,
          hasParent: false,
          hasChildren: true,
          children: []
        }
      ],
      treeProps: {
        children: 'children',
        label: 'name'
      },
      treeForm: {
        id: '',
        name: ''
      },
      currentId1: '',
      prefixUrl: '',
      delGroupId: ''
    }
  },
  created() {
    this.init()
    this.handleQuery()
  },
  methods: {
    init() {
      this.getCategory0()
    },
    getCategory0() {
      categoryList({ levelType: 2, mediaType: 4}).then((res) => {
        if (res.code === 200) {
          const { data } = res
          this.treeData1[0].children = data
        }
      })
    },
    handleNodeClick1(data) {
      this.categoryId = data.id
      this.queryForm.pageNum = 1
      this.handleQuery()
    },
    itemEdit1(node, data) {
      this.treeForm.id = data.id
      this.treeForm.name = data.name
      this.companyFlag = true
      this.treeDialogVisible = true
    },
    itemDel1(node, data) {
      this.delVisible = true
      console.log('data.id', data.id)
      this.delGroupId = data.id
    },
    handleAddItem1() {
      this.companyFlag = true
      this.treeDialogVisible = true
    },
    // 添加
    treeSubmit() {
      // 编辑
      if (this.treeForm.id) {
        categoryEditCorp({
          id: this.treeForm.id,
          // levelType: this.companyFlag ? 0 : 1,
          levelType: 2,
          mediaType: 4,
          name: this.treeForm.name
        }).then((res) => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.treeDialogVisible = false
            this.treeForm.name = ''
            this.treeForm.id = ''
            this.getCategory0()
            // this.companyFlag ? this.getCategory0() : this.getCategory1();
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        // 新增
        categoryAddCorp({
          levelType: 2,
          // levelType: this.companyFlag ? 0 : 1,
          mediaType: 4,
          name: this.treeForm.name
        }).then((res) => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.treeDialogVisible = false
            this.treeForm.name = ''
            this.treeForm.id = ''
            this.getCategory0()
            // this.companyFlag ? this.getCategory0() : this.getCategory1();
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
    // 删除
    treeDelSubmit() {
      categoryDel(this.delGroupId).then((res) => {
        this.delVisible = false
        if (res.code === 200) {
          this.$message.success(res.data)
          // this.getCategory1();
        } else {
          this.$message.error(res.msg)
        }
        this.getCategory0()
      })
    },
    // 重置
    handleReset() {
      this.queryForm = {
        riskName: '',
        deptId: null,
        createCodeList: '',
        pageNum: 1,
        pageSize: 20,
      }
      this.handleQuery()
    },
    // 远程搜索任务名称 + 搜索
    handleQuery() {
      let obj = {
        categoryId: this.categoryId,
        levelType: 2,
        deptId: this.queryForm.deptId,
        riskName: this.queryForm.riskName,
        createCodeList: this.queryForm.createCodeList ? this.queryForm.createCodeList + '' : null,
        pageNum: this.queryForm.pageNum,
        pageSize: this.queryForm.pageSize,
      }
      this.loading = true
      this.tableList = []
      riskList(obj).then((res) => {
        if (res.code === 200) {
          this.tableList = res.data.rows || []
          this.total = res.data.total || 0
        }
        this.loading = false
      })
    },
    // 翻页
    handleSizeChange(val) {
      this.queryForm.pageNum = 1
      this.queryForm.pageSize = val
      this.handleQuery()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum = val
      this.handleQuery()
    },
    selectOptions(obj) {
      // 执行员工
      this.queryCreateList = obj.select
    },
    // 查询执行员工
    queryDeptMsg() {
      queryDeptMsg().then((res) => {
        if (res.code === 200 && res.data.length) {
          res.data.forEach((item) => {
            this.staffOptions.push({
              id: item.deptId,
              label: item.deptName,
              children: null
            })
          })
        }
      })
    },
    // 懒加载 执行员工,创建人
    loadOptions({ action, parentNode, callback }) {
      queryUserMsg({ deptId: parentNode.id }).then((res) => {
        if (res.code === 200 && res.data.length) {
          parentNode.children = []
          res.data.forEach((item) => {
            parentNode.children.push({
              id: item.userId,
              label: item.userName
            })
          })
          callback()
        }
      })
    },
    // 选中任务名称
    handleSelect(item) {
      this.query.riskName = item.value
    },
    // 状态修改
    switchChange(index, event) {
      this.tableList[index].status = event
      let obj = {
        riskId: this.tableList[index].riskId,
        status: event
      }
      updateRuleStatus(obj).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.data)
        }
      })
    },
    // 分享
    shareRoute() {
      let clipboard = new this.clipboard('.tag-copy')
      clipboard.on('success', (e) => {
        this.$message({
          message: '复制链接成功!',
          type: 'success',
          duration: 2000
        })
        //清除选中
        e.clearSelection()
        //释放内存，以防重复复制
        clipboard.destroy()
      })
      clipboard.on('error', (e) => {
        // 不支持复制
        this.$message({
          message: '复制链接失败!',
          type: 'error',
          duration: 2000
        })
        //清除选中
        e.clearSelection()
        //释放内存，以防重复复制
        clipboard.destroy()
      })
    },

    // 新增
    goRoute() {
      this.$router.push({
        name: 'AddInsurance',
        query: { operateFlag: 'ADD' }
      })
    },
    // 详情
    detailRoute(id) {
      this.$router.push({
        path: 'detailInsurance',
        query: { riskId: id, operateFlag: 'DETAIL' }
      })
    },
    // 编辑
    editRoute(id) {
      this.$router.push({
        path: 'editInsurance',
        query: { riskId: id, operateFlag: 'EDIT' }
      })
    },
    // 删除
    deleteItem(id) {
      this.delTable = true
      this.riskId = id
    },
    deleteLink() {
      deleteLink({ riskIds: this.riskId }).then((res) => {
        if (res.code === 200) {
          this.cancelDel()
          this.handleQuery()
        }
      })
    },
    // 取消删除
    cancelDel() {
      this.delTable = false
      this.riskId = ''
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/base.scss";

.table-img {
  width: 50px;
  height: 50px;
  margin: 0 auto;
  padding-top: 10px;
  box-sizing: border-box;

  img {
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
  }
}
::v-deep .el-table th.el-table__cell.is-leaf,
.el-table td.el-table__cell {
  border-bottom: none !important;
}

::v-deep .el-table .el-table__header-wrapper th {
  background-color: #f8f8f9 !important;
}

.insuranceBg {
  background: #fff;

  ::v-deep {
    .el-tabs__header {
      margin: 0;
    }

    .el-tree {
      padding: 10px 10px 0 10px;
    }

    .el-tree-node__content {
      height: 35px;
      padding-left: 10px !important;
    }
  }
}

.custom-tree-node {
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding-right: 10px;
  font-size: 12px;

  .label-info {
    width: 100px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

.add {
  color: #082aff;
}
</style>
